RAIS  3.2
C:/Projekte/RAIS/UI Common/Localisation.cs
Go to the documentation of this file.
00001 
00027 using System;
00028 using System.Collections.Generic;
00029 using System.Text;
00030 using System.Web.UI;
00031 using System.Web.UI.WebControls;
00032 
00033 using RAIS.Common.UserManagement;
00034 using RAIS.ApplicationLogicLayer;
00035 using RAIS.Common.Localization;
00036 
00037 using Pages = System.Collections.Generic.Dictionary<string, RAIS.UI.Common.Localisation.Page>;
00038 using Cultures = System.Collections.Generic.Dictionary<string, RAIS.Common.Localization.Culture>;
00039 using Contexts = System.Collections.Generic.Dictionary<string, RAIS.Common.Localization.Context>;
00040 
00041 namespace RAIS.UI.Common
00042 {
00046     public class Localisation
00047     {
00051         public class Page
00052         {
00053             #region Fields
00054             string currentLaguage;                  
00055             #endregion
00056             #region Properties
00057 
00060             public string CurrentLanguage
00061             {
00062                 get { return this.currentLaguage; }
00063                 set { this.currentLaguage = value; }
00064             }
00065             #endregion
00066         }
00067         #region Public static methods
00068 
00072         public static void InitializeContextsList(ListItemCollection items)
00073         {
00074             Contexts contexts = RAIS.ApplicationLogicLayer.Localisation.Contexts;
00075 
00076             List<Context> contextList = new List<Context>();
00077 
00078             foreach (Context context in contexts.Values)
00079                 contextList.Add(context);
00080 
00081             contextList.Sort((x, y) => x.VisibleName.CompareTo(y.VisibleName));
00082 
00083             string[] contextNames = new string[contexts.Count];
00084             string[] contextValues = new string[contexts.Count];
00085 
00086             for(int i=0;i<contextList.Count;i++)
00087             {
00088                 contextNames[i] = contextList[i].VisibleName;
00089                 contextValues[i] = contextList[i].Name;
00090             }
00091 
00092             UI.Common.Controls.InitializeList(items, contextNames, contextValues);
00093         }
00098         public static void InitializeCulturesList(DropDownList list)
00099         {
00100             bool emptyList = list.Items.Count == 0;
00101 
00102             System.Globalization.CultureInfo[] cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures); //.NeutralCultures);
00103 
00104             string[] languages = new string[cultures.Length];
00105             for (int i = 0; i < cultures.Length; i++)
00106             {
00107                 languages[i] = cultures[i].NativeName;
00108             }
00109 
00110             UI.Common.Controls.InitializeList(list.Items, languages);
00111 
00112             if (emptyList)
00113             {
00114                 if (!SelectCurrentSystemLnaguage(list))
00115                 {
00116                     list.SelectedValue = RAIS.Common.Constants.DEFAULT_LANGUAGE;
00117                 }
00118             }
00119 
00120             for (int i = 0; i < cultures.Length; i++)
00121             {
00122                 list.Items[i].Value = cultures[i].LCID.ToString();
00123             }
00124 
00125         }
00130         private static bool SelectCurrentSystemLnaguage(DropDownList list)
00131         {
00132             System.Globalization.CultureInfo sessionCulture = new System.Globalization.CultureInfo(list.Page.Session.LCID);
00133             Cultures supportedLanguages = RAIS.ApplicationLogicLayer.Localisation.SupportedLanguages;
00134             if (supportedLanguages.ContainsKey(sessionCulture.NativeName))
00135             {
00136                 list.SelectedValue = sessionCulture.NativeName;
00137                 return true;
00138             }
00139             else
00140                 return false;
00141         }
00146         public static void InitializeLanguagesSelection(ListItemCollection items)
00147         {
00148             Cultures supportedLanguages = RAIS.ApplicationLogicLayer.Localisation.SupportedLanguages;
00149 
00150             string[] languages = new string[supportedLanguages.Count];
00151             supportedLanguages.Keys.CopyTo(languages, 0);
00152 
00153             UI.Common.Controls.InitializeList(items, languages);
00154         }
00158         public static void CleanTranslations(System.Web.SessionState.HttpSessionState session)
00159         {
00160             Pages pages = GetPagesColleciton(session);
00161 
00162             foreach (Localisation.Page page in pages.Values)
00163             {
00164                 page.CurrentLanguage = RAIS.Common.Constants.DEFAULT_LANGUAGE;
00165             }
00166         }
00172         public static void TranslatePage(System.Web.UI.Page page)
00173         {
00174             UserData currentUser = (UserData)page.Session[UI.Common.Constants.CURRENT_USER_TAG];
00175             if( currentUser != null )
00176                 TranslatePageToLanguage(page, currentUser.Language);
00177         }
00184         public static void TranslatePageToLanguage(System.Web.UI.Page page, string targetLanguage)
00185         {
00187             //if (page.ToString() == "ASP.loginpage_aspx")
00188             //{
00189             //    TranslateCaptions(page.Controls, GetPageCurrentLanguage(page), targetLanguage);
00190             //    SetPageCurrentLanguage(page, targetLanguage);
00191             //}
00192             //else
00193             //{
00194             //    //if (page.IsPostBack) return;
00195             //    TranslateCaptions(page.Controls, RAIS.Common.Constants.DEFAULT_LANGUAGE, targetLanguage);
00196             //    SetPageCurrentLanguage(page, targetLanguage);
00197             //}
00198 
00199             TranslateCaptions(page.Controls, GetPageCurrentLanguage(page), targetLanguage);
00200             SetPageCurrentLanguage(page, targetLanguage);
00201 
00202         }
00212         public static void TranslateCaptions(ControlCollection controls, string sourceLanguage, string targetLanguage)
00213         {
00214             //if (sourceLanguage != targetLanguage)
00215             {
00216                 foreach (Control control in controls)
00217                 {
00218                     string translationContext = control.Page.ToString();
00219                     if (control is LinkButton)
00220                     {
00221 
00222                         if (!(control.Parent is DataControlFieldCell || control.Parent.ClientID.Contains("GVlist"))) //skipping Grid values (user data): issue #751
00223                         {
00224                             LinkButton button = (LinkButton) control;
00225                             button.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(button.Text,
00226                                                                                             translationContext,
00227                                                                                             sourceLanguage,
00228                                                                                             targetLanguage);
00229                         }
00230                     }
00231                     else
00232                     if (control is Label)
00233                     {
00234                         if (!(control.ID == "LBLselectedDepartmentNameNoTranslation" ||
00235                               control.ID == "LBLselectedFacilityNameNoTranslation" ||
00236                               control.ID == "LBLusernameNoTranslation" ||
00237                               control.ID == "LBLrestrictionNameNoTranslation" ||
00238                               control.ID == "LBLicsrsUserNameNoTranslation"))     // skipping user data: issue #751
00239                         {
00240                             Label label = (Label)control;
00241                             label.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(label.Text,
00242                                                                                            translationContext,
00243                                                                                            sourceLanguage,
00244                                                                                            targetLanguage);
00245                         }
00246                     }
00247                     else
00248                     if (control is Button)
00249                     {
00250                         Button button = (Button)control;
00251                         button.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(    button.Text,
00252                                                                                             translationContext,
00253                                                                                             sourceLanguage, 
00254                                                                                             targetLanguage);
00255                     }
00256                     else
00257                     if (control is RadioButton)
00258                     {
00259                         RadioButton button = (RadioButton)control;
00260                         button.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(    button.Text,
00261                                                                                             translationContext,
00262                                                                                             sourceLanguage, 
00263                                                                                             targetLanguage);
00264                     }
00265                     else
00266                     if (control is CheckBox)
00267                     {
00268                         CheckBox button = (CheckBox)control;
00269                         button.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(button.Text,
00270                                                                                         translationContext,
00271                                                                                         sourceLanguage,
00272                                                                                         targetLanguage);
00273                     }
00274                     else
00275                     if (control is RadioButtonList)
00276                     {
00277                         RadioButtonList list = (RadioButtonList)control;
00278                         ListItemCollection items = list.Items;
00279                         foreach (ListItem item in items)
00280                         {
00281                             item.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(  item.Text,
00282                                                                                             translationContext,
00283                                                                                             sourceLanguage,
00284                                                                                             targetLanguage);
00285                         }
00286                     }
00287                     else
00288                     if (control is TreeView)
00289                     {
00290                         TreeView tree = (TreeView)control;
00291                         TreeNodeCollection nodes = tree.Nodes;
00292 
00293                         TranslateTreeNodes(nodes, tree.ID, sourceLanguage, targetLanguage); 
00294                     }
00295 
00296                     TranslateCaptions(control.Controls, sourceLanguage, targetLanguage);//Recursion
00297                 }
00298             }
00299         }
00300 
00301 
00302         public static void TranslateGridViewHeader(GridView GV, string translationContext, string sourceLanguage, string targetLanguage)
00303         {
00304             foreach (DataControlField gridColumn in GV.Columns)
00305             {
00306                 gridColumn.HeaderText = RAIS.ApplicationLogicLayer.Localisation.Translate(
00307                     gridColumn.HeaderText,
00308                     translationContext,
00309                     sourceLanguage,
00310                     targetLanguage);
00311             }
00312         }
00313 
00314 
00315         #endregion
00316         #region Private static helper methods
00317         private static void  TranslateTreeNodes(TreeNodeCollection nodes, string translationContext, string sourceLanguage, string targetLanguage)
00318         {
00319             foreach (TreeNode node in nodes)
00320             {
00321                 node.ImageToolTip = node.Text;     // used to store the untranslated node text
00322                 node.Text = RAIS.ApplicationLogicLayer.Localisation.Translate(node.Text, translationContext, sourceLanguage, targetLanguage);
00323                 if( node.ChildNodes.Count > 0 )
00324                     TranslateTreeNodes(node.ChildNodes, translationContext, sourceLanguage, targetLanguage);
00325             }
00326         }
00332         private static string GetPageCurrentLanguage(System.Web.UI.Page page)
00333         {
00334             Pages pages = GetPagesColleciton(page.Session);
00335 
00336             string pageString = page.ToString();
00337             if (pages != null && pages.ContainsKey(pageString))
00338                 return pages[pageString].CurrentLanguage;
00339             else
00340                 return RAIS.Common.Constants.DEFAULT_LANGUAGE; ;
00341         }
00347         private static void SetPageCurrentLanguage(System.Web.UI.Page page, string currentLanguage)
00348         {
00349             Pages pages = GetPagesColleciton(page.Session);
00350             string pageString = page.ToString();
00351 
00352             if ( !pages.ContainsKey(pageString))
00353                 pages[pageString] = new Localisation.Page();
00354 
00355             pages[pageString].CurrentLanguage = currentLanguage;
00356         }
00362         private static Pages GetPagesColleciton(System.Web.SessionState.HttpSessionState session)
00363         {
00364             if (session[Constants.PAGES_TAG] == null)
00365                 session[Constants.PAGES_TAG] = new Pages();
00366 
00367             return (Pages)session[Constants.PAGES_TAG];
00368         }
00369         #endregion
00370     }
00371 }